#pragma once

#include <string>
#include <vector>

#include <xpam/misc/nocopy.h>
#include <iostream>


namespace xpam
{
    namespace net
    {
		extern int infinite;

        class socket : public xpam::misc::nocopy
        {
        private:
            int _socket;
            bool _is_server;

        public:
            socket(void);
            ~socket(void);
            
            void bind(const std::string & host, int port);
            void accept(xpam::net::socket & sock);
            void connect(const std::string & host, int port);
            void close();

            int send(const void * data, size_t length);
            int recv(void * data, size_t length);
            void recv_full(void * data, size_t length);
            
            bool select(int interval);
            static std::vector<socket*> select(const std::vector<socket*> & socks, int interval);

            bool is_server() { return _is_server; }
            bool is_connected();
        };

        class text_writer
        {
            socket & _socket;
        public:
            text_writer(socket & s) : _socket(s)
            { }

            text_writer & operator << (const std::string & str);
            text_writer & operator << (const char * str);
        };
            
        class text_reader
        {
            socket & _socket;
        public:
            text_reader(socket & s) : _socket(s)
            { }

            void readline(std::string & str, char stop = '\n');
        };
    }
}